package com.gxa.exchangerate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.R;
import com.gxa.common.entity.financial_management.AccountList;
import com.gxa.common.entity.financial_management.RechargeManagement;
import com.gxa.exchangerate.feign.RechargeManagementFeign;
import com.gxa.exchangerate.mapper.RechargeManagementMapper;
import com.gxa.exchangerate.service.RechargeManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class RechargeManagementServiceImpl extends ServiceImpl<RechargeManagementMapper, RechargeManagement> implements RechargeManagementService {

    @Autowired
    private RechargeManagementMapper rechargeManagementMapper;

    @Resource
    private RechargeManagementFeign rechargeManagementFeign;

    @Cacheable(value = "rechargeManagementList",key = "'selectRechargeManagementList'")
    @Override
    public R selectRechargeManagementList(Integer currentPage, Integer pageSize) {
        Page page = new Page(currentPage,pageSize);
        Page selectPage = rechargeManagementMapper.selectPage(page, null);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map rechargeManagementMap = new HashMap();
            rechargeManagementMap.put("totalSize",selectPage.getTotal());
            rechargeManagementMap.put("totalPage",selectPage.getPages());
            rechargeManagementMap.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(rechargeManagementMap);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    @Override
    public R selectRechargeManagement(Integer currentPage, Integer pageSize, RechargeManagement rechargeManagement) {
        Page page = new Page(currentPage,pageSize);
        QueryWrapper qw = new QueryWrapper();
        qw.eq(rechargeManagement.getTransactionNo() != null,"id",rechargeManagement.getTransactionNo())
                .eq(rechargeManagement.getAccountType() != null,"account_type",rechargeManagement.getAccountType())
                .eq(rechargeManagement.getNickname() != null,"nickname",rechargeManagement.getNickname())
                .eq(rechargeManagement.getPhone() != null,"phone",rechargeManagement.getPhone())
                .eq(rechargeManagement.getStatus() != null,"status",rechargeManagement.getStatus())
                .between(rechargeManagement.getStartTime() != null&&rechargeManagement.getEndTime() != null,"create_time",rechargeManagement.getStartTime(),rechargeManagement.getEndTime())
                .lt(rechargeManagement.getStartTime() == null && rechargeManagement.getEndTime() != null,"create_time",rechargeManagement.getEndTime());
        Page selectPage = rechargeManagementMapper.selectPage(page, qw);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map rechargeManagementMap = new HashMap();
            rechargeManagementMap.put("totalSize",selectPage.getTotal());
            rechargeManagementMap.put("totalPage",selectPage.getPages());
            rechargeManagementMap.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(rechargeManagementMap);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    //添加
    @Override
    public R addRechargeManagement(RechargeManagement rechargeManagement) {
        AccountList accountList = rechargeManagementFeign.selectAccountByPhone(rechargeManagement.getPhone());
        rechargeManagement.setOldAccountBalance(accountList.getAccountBalance());

        return null;
    }
}
